package cn.source.authorizaton.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.source.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.authorizaton.domain.AuthorizedDevice;
import cn.source.authorizaton.service.IAuthorizedDeviceService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * authorizatonController
 * 
 * @author sourcebyte.vip
 * @date 2025-05-31
 */
@RestController
@RequestMapping("/system/deviceAuthorized")
public class AuthorizedDeviceController extends BaseController
{
    @Autowired
    private IAuthorizedDeviceService authorizedDeviceService;

    /**
     * 查询authorizaton列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AuthorizedDevice authorizedDevice)
    {
        startPage();
        List<AuthorizedDevice> list = authorizedDeviceService.selectAuthorizedDeviceList(authorizedDevice);
        return getDataTable(list);
    }

    /**
     * 导出authorizaton列表
     */
    @Log(title = "authorizaton", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AuthorizedDevice authorizedDevice)
    {
        List<AuthorizedDevice> list = authorizedDeviceService.selectAuthorizedDeviceList(authorizedDevice);
        ExcelUtil<AuthorizedDevice> util = new ExcelUtil<AuthorizedDevice>(AuthorizedDevice.class);
        util.exportExcel(response, list, "authorizaton数据");
    }

    /**
     * 获取authorizaton详细信息
     */
    @GetMapping(value = "/{authorizerId}")
    public AjaxResult getInfo(@PathVariable("authorizerId") Long authorizerId)
    {
        return AjaxResult.success(authorizedDeviceService.selectAuthorizedDeviceByAuthorizerId(authorizerId));
    }

    /**
     * 新增authorizaton
     */
    @Log(title = "authorizaton", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<AuthorizedDevice> authorizedDevice)
    {
        for (AuthorizedDevice device : authorizedDevice){
            device.setAuthorizerId(SecurityUtils.getUserId());
        }
        return toAjax(authorizedDeviceService.insertAuthorizedDevice(authorizedDevice));
    }

    /**
     * 修改authorizaton
     */
    @Log(title = "authorizaton", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuthorizedDevice authorizedDevice)
    {
        return toAjax(authorizedDeviceService.updateAuthorizedDevice(authorizedDevice));
    }

    /**
     * 删除authorizaton
     */
    @PreAuthorize("@ss.hasPermi('system:deviceAuthorized:remove')")
    @Log(title = "authorizaton", businessType = BusinessType.DELETE)
	@DeleteMapping("/cancelAuthorized")
    public AjaxResult remove(AuthorizedDevice authorizedDevice)
    {
        return toAjax(authorizedDeviceService.deleteAuthorizedDeviceByAuthorizerId(authorizedDevice));
    }
}
